// import chalk from 'chalk';
import { dummyLogger, Logger } from "ts-log";

type levelType = 'debug' | 'info' | 'warn' | 'error';

type textType = string | object;

export class LogManager {

    private _level: levelType = 'debug';
    private _history: string[] = [];

    public constructor(private readonly log: Logger = dummyLogger) { }

    static init(_jees: any) {
        _jees.LOG = new LogManager(console);
        // print test
        _jees.LOG.debug('debug bingo!');
        _jees.LOG.info('info  bingo!');
        _jees.LOG.warn('warn  bingo!');
        _jees.LOG.error('error bingo!');
    }

    public level(_l: levelType) {
        this._level = _l;
    }

    public debug(_text: textType, ...p: textType[]) {
        return this.print('debug', _text, ...p);
    }

    public info(_text: textType, ...p: textType[]) {
        return this.print('info', _text, ...p);
    }

    public warn(_text: textType, ...p: string[]) {
        return this.print('warn', _text, ...p);
    }

    public error(_text: textType, ...p: textType[]) {
        return this.print('error', _text, ...p);
    }

    // public debug(_text: string, ...p: string[]) {
    //     this.print('debug', _text, p);
    // }

    // public log(_text: string, ...p: string[]) {
    //     this.print('log', _text, p);
    // }

    // public warn(_text: string, ...p: string[]) {
    //     this.print('warn', _text, p);
    // }

    // public error(_text: string, ...p: string[]) {
    //     this.print('error', _text, p);
    // }

    public print(_type: levelType, _text: textType, ...p: textType[]) {
        let pp: any = null;
        let nn: number = this._check_level(_type);
        let _text_full: string | null = null;
        switch (_type) {
            case 'debug':
                // if (nn <= 0) pp = chalk.dim; // 灰色
                if (nn <= 0) {
                    _text_full = `| DEBUG | >>> ${_text}`;
                }
                break;
            case 'info':
                // if (nn <= 1) pp = chalk.bold.green; // 字体绿色
                if (nn <= 1) {
                    _text_full = `| INFO  | >>> ${_text}`;
                }
                break;
            case 'warn':
                // if (nn <= 2) pp = chalk.hex('#FFA500'); // 自定义橙色
                if (nn <= 2) {
                    _text_full = `| WARN  | >>> ${_text}`;
                }
                break;
            case 'error':
                // if (nn <= 3) pp = chalk.bgRed; // 背景红色
                if (nn <= 3) {
                    _text_full = `| ERROR | >>> ${_text}`;
                }
                break;
            default:
                break;
        }
        if (_text_full) {
            // console.log(pp(_text), p);
            this.log.info(_text_full, ...p);
        }
        return _text_full;
    }

    private _check_level(_l: levelType): number {
        _l = this._level;
        let n =
            _l === 'debug' ? 0 : (
                _l === 'info' ? 1 : (
                    _l === 'warn' ? 2 : (
                        _l === 'error' ? 3 : 4
                    )
                )
            );
        return n;
    }

}